//
//  SDCitySelectVM.swift
//  BotuPanoram
//
//  Created by slowdony on 2019/11/19.
//  Copyright © 2019 Bicon. All rights reserved.
//

import UIKit
import SwiftyJSON

class SDCitySelectVM: NSObject {
    
    lazy var hotCityArr: [JSON] = {
        let hotCityArr = Array<JSON>.init()
        return hotCityArr
    }()
    
    lazy var allCityArr:[[SDCityModel]] = {
        let allCityArr = Array<Array<SDCityModel>>.init()
        return allCityArr
    }()

    lazy var firstLetterArray: [String] = {
           let firstLetterArray = ["热门城市"]
           return firstLetterArray
    }()
    
  
    
    func getHotCityByNetwork(callBack:@escaping ((Bool)->())){
        
        Network.request(.getHotcity) { JSON in
           
            self.hotCityArr=JSON["data"]["hotCity"].array ?? []
            let  allArray=JSON["data"]["cityData"].array ?? []
            
            
            if allArray.count > 0 {
                
                for dic in allArray {
                    let letter =  dic["keyword"].string ?? ""
                    self.firstLetterArray.append(letter)
                    
                    let infoList=dic["infoList"].array ?? []
                    var letterArray:Array<SDCityModel>=[]
                    
                    for mdic in infoList{
                        let cityModel =  SDCityModel()
                        cityModel.pinyin = mdic["pinyin"].string as NSString?
                        cityModel.code = mdic["code"].string
                        cityModel.city = mdic["city"].string
                        letterArray.append(cityModel)
                    }
                   
                    self.allCityArr.append(letterArray)
                    
                }
            }
            
            callBack(true)
            
        } failure: { err in
            
        }

    }
    
    
    
    
   
}

extension Array where Element: Hashable {
    func removingDuplicates() -> [Element] {
        var addedDict = [Element: Bool]()

        return filter {
            addedDict.updateValue(true, forKey: $0) == nil
        }
    }
    mutating func removeDuplicates() {
        self = self.removingDuplicates()
    }
}
